/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.futco.sysconthos.Modelo.Procesos.Daos;

import org.futco.sysconthos.Modelo.Admisiones.Daos.IDAO;

import org.futco.sysconthos.Modelo.Admisiones.Daos.Model;
import org.futco.sysconthos.Modelo.Proceso.Cita;

import org.futco.sysconthos.Modelo.Proceso.HistoriaClinica;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author randy fernando
 */
public class HistoriaClinicaDAO implements IDAO<HistoriaClinica>{

     private Model model;
    private PreparedStatement psInsert;

    public HistoriaClinicaDAO() throws SQLException {
        model = Model.getInstance();
    }



    public void insert(HistoriaClinica hc) throws SQLException {
       if (psInsert == null) {
            String sqlInsert = "INSERT INTO historiaclinica (codigo,Fecha,codCita,MotivoConsulta,EnfermedadActual,Peso,Estatura,TipoSangre,PresionArterial,Edad,Sintomas,"
                    + "AToxicos,AFarmacologicos,AQuirurgicos,APatologicos,Alergias,AOtros) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
    
     
        psInsert.setString(1, hc.getCodigo());
        psInsert.setString(2,hc.getFechaHC() );
        psInsert.setString(3, hc.getCodCita());
        psInsert.setString(4, hc.getMotivoConsulta());
        psInsert.setString(5, hc.getEnfermedadActual());
        psInsert.setString(6, hc.getPeso());
        psInsert.setString(7, hc.getEstatura());
        psInsert.setString(8, hc.getTipoSangre());
        psInsert.setString(9, hc.getPresion_arterial());
        psInsert.setString(10, hc.getEdad());
        psInsert.setString(11, hc.getSintomas());
        psInsert.setString(12,hc.getAntecedentesQuirurgicos());
        psInsert.setString(13,hc.getAntecedentesToxicos());
        psInsert.setString(14,hc.getAntecedentesPatologicos());
        psInsert.setString(15,hc.getAntecedentesFarmacologicos());
        psInsert.setString(16,hc.getAntecedentesOtros());
        psInsert.setString(17,hc.getAlergias());
     
        psInsert.executeUpdate();
        }
    }


    public List<HistoriaClinica> getAll() throws SQLException {
        List lista = new LinkedList<HistoriaClinica>();
        java.sql.PreparedStatement pst = null;
        ResultSet rs = null;
        HistoriaClinica hc = null;
        if(!model.isConnect())
        {
            throw new SQLException(":V  historia");
        }
        try{
            pst = model.getCon().prepareStatement("SELECT * FROM historiaclinica");
            rs = pst.executeQuery();
            if(rs==null){
                throw new SQLException("No hay historia clinica registrada");
            }
            while(rs.next())
            {
                lista.add(load(rs));
            }
        } finally {
            if (rs != null) {
               rs.close();
           }
            if (pst != null) {
              pst.close();
           }
       }

        return lista;
    }

    public HistoriaClinica find(String codhc) throws SQLException {
        java.sql.PreparedStatement pst =  null;
        HistoriaClinica hc  = null;
        ResultSet rs = null;
        if(!model.isConnect())
        {
            throw new SQLException("no hay coneccion");
        }
        pst = model.getCon().prepareStatement("Select * from historiaclinica where codigo = ?");
        pst.setString(1, codhc);
        rs = pst.executeQuery();
        if(rs != null)
        {
            hc = load(rs);

        }
        if(rs != null)
        {
            model.getCon().close();
        }
        pst.close();
        rs.close();
        return hc;
    }


    public void delete(HistoriaClinica t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(HistoriaClinica t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    public List<HistoriaClinica> getAllBy(String parametro,String name) throws SQLException {
        List lista = new LinkedList<HistoriaClinica>();
        java.sql.PreparedStatement pst = null;
        ResultSet rs = null;
        HistoriaClinica hcl = null;
           

        try {
            pst = model.getCon().prepareStatement("SELECT * FROM historiaclinica WHERE " +parametro + " like ?");
            pst.setString(1, "%" + name.trim() + "%");
            rs = pst.executeQuery();

            while (rs.next()) {
                hcl = new HistoriaClinica();

                hcl.setCodigo(rs.getString(1));
                hcl.setFechaHC(rs.getString(2));
                hcl.setCodCita(rs.getString(3));
                hcl.setMotivoConsulta(rs.getString(4));
                hcl.setEnfermedadActual(rs.getString(5));
                hcl.setPeso(rs.getString(6));
                hcl.setEstatura(rs.getString(7));
                hcl.setTipoSangre(rs.getString(8));
                hcl.setPresion_arterial(rs.getString(9));
                hcl.setEdad(rs.getString(rs.getString(10)));
                hcl.setSintomas(rs.getString(11));
                hcl.setAntecedentesToxicos(rs.getString(12));
                hcl.setAntecedentesFarmacologicos(rs.getString(13));
                hcl.setAntecedentesQuirurgicos(rs.getString(14));
                hcl.setAntecedentesPatologicos(rs.getString(15));
                hcl.setAlergias(rs.getString(16));
                hcl.setAntecedentesOtros(rs.getString(17));
                
                lista.add(hcl);
               
            }

        } finally {
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return lista;
    }
    public HistoriaClinica consultarPorCodigoCita(String codCita) throws SQLException{
        HistoriaClinica hc = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        if(!model.isConnect())
        {
            throw new SQLException("no hay coneccion");
        }
        try{
            pst = (PreparedStatement) model.getCon().prepareStatement("SELECT * FROM historiaclinica WHERE codCita = ?");
            pst.setString(1, codCita.trim());
            rs = pst.executeQuery();
            if(rs.next())
            {
                hc = this.load(rs);
            }
        }finally{
            if (pst != null) {
                    pst.close();
            }
            if (rs != null) {
                rs.close();
            }
        }

        return hc;

    }
     public List listaCitasDePaciente(String codPaciente) throws SQLException {
        List lista = new LinkedList();
        java.sql.PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = model.getCon().prepareStatement("SELECT * FROM cita WHERE codPaciente = ? GROUP BY codigo");
            pst.setString(1,  codPaciente.trim());
            rs = pst.executeQuery();
            while (rs.next()) {
                lista.add(load(rs));
            }

        } finally {
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return lista;
    }
    public HistoriaClinica load(ResultSet rs) throws SQLException{
        HistoriaClinica hc = new HistoriaClinica();
        
        hc.setCodigo(rs.getString(1));
        hc.setFechaHC(rs.getString(2));
        hc.setCodCita(rs.getString(3));
        hc.setMotivoConsulta(rs.getString(4));
        hc.setEnfermedadActual(rs.getString(5));
        hc.setPeso(rs.getString(6));
        hc.setEstatura(rs.getString(7));
        hc.setTipoSangre(rs.getString(8));
        hc.setPresion_arterial(rs.getString(9));
        hc.setEdad(rs.getString(10));
        hc.setSintomas(rs.getString(11));
        hc.setAntecedentesToxicos(rs.getString(12));
        hc.setAntecedentesFarmacologicos(rs.getString(13));
        hc.setAntecedentesQuirurgicos(rs.getString(14));
        hc.setAntecedentesPatologicos(rs.getString((15)));
        hc.setAlergias(rs.getString(16));
        hc.setAntecedentesOtros(rs.getString(17));
            
        return hc;
    }

    public HistoriaClinica find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(String codigo, HistoriaClinica t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
